<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">
      <el-form :inline="true" status-icon label-position="left" class="demo-form-inline" >
        <el-form-item label="账户号" prop="account">
          <el-input v-model="listQuery.account" placeholder="账户号"/>
        </el-form-item>
        <el-form-item label="账户名称" prop="name">
          <el-input v-model="listQuery.name" placeholder="账户名称"/>
        </el-form-item>
        <el-form-item label="开户银行编码" prop="bankCode">
          <el-input v-model="listQuery.bankCode" placeholder="开户银行编码"/>
        </el-form-item>
        <!--<el-form-item label="开户银行联行号" prop="interBankNo">
          <el-input v-model="listQuery.interBankNo" placeholder="开户银行联行号"/>
        </el-form-item>-->
        <el-form-item label="开户银行名称" prop="bankName">
          <el-input v-model="listQuery.bankName" placeholder="开户银行名称"/>
        </el-form-item>
        <el-form-item label="账户类型" prop="accType">
          <el-select v-model="listQuery.accType" placeholder="请选择">
            <el-option value="0" label="服务费账户">服务费账户</el-option>
            <el-option value="1" label="代付账户">代付账户</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否使用" prop="used">
          <el-select v-model="listQuery.used" placeholder="请选择">
            <el-option value="" label="全部">全部</el-option>
            <el-option value="0" label="否">否</el-option>
            <el-option value="1" label="是">是</el-option>
          </el-select>
          <!--<el-radio-group v-model="listQuery.used">-->
            <!--<el-radio :label="0">否</el-radio>-->
            <!--<el-radio :label="1">是</el-radio>-->
          <!--</el-radio-group>-->
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" @click="queryList">查询</el-button>
          <el-button class="filter-item" type="primary" icon="el-icon-edit"
                     @click="handleAdd">新增
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 测试提交2 -->

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              :header-cell-style="{background:'#92D3FF',color:'#fff'}">

      <el-table-column align="center" min-width="100" label="账户号" prop="account"/>

      <el-table-column align="center" min-width="100" label="账户名称" prop="name"/>
      <el-table-column align="center" min-width="100" label="银行名称" prop="bankCode"/>
     <!-- <el-table-column align="center" min-width="100" label="开户银行联行号" prop="interBankNo"/>-->
      <el-table-column align="center" min-width="100" label="开户银行名称" prop="bankName"/>
      <el-table-column align="center" min-width="100" label="账户类型" prop="accType">
        <template slot-scope="scope">
          <el-tag>{{{"0":"服务费账户","1":"代付账户"}[scope.row.accType]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="是否使用" prop="used">
        <template slot-scope="scope">
          <el-tag>{{{"0":"否","1":"是"}[scope.row.used]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini"
                     @click="handleEdit(scope.row)">修改
          </el-button>
          <el-popover
            placement="top"
            width="200"
            trigger="manual"
            :ref="'popover-' + scope.row.account">
            <p>确定删除该数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.account].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click="doDelete(scope.row)">确定</el-button>
            </div>
            <el-button type="danger" size="mini" slot="reference" @click="$refs[`popover-` + scope.row.account].doShow()">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"-->
    <!--@pagination="getList"/>-->
    <!-- 分配订单对话框 -->
    <el-dialog :visible.sync="editDialogVisible" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="账户号" prop="account">
          <el-input :disabled="dialogStatus=='update'" v-model="editForm.account" placeholder="账户号"/>
        </el-form-item>
        <el-form-item label="账户名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off" placeholder="账户名称"/>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankCode">
          <el-select @change="bankNameChange" v-model="editForm.bankCode" placeholder="请选择" filterable style="width:100%">
            <el-option
              v-for="item in banks"
              :key="item.bankName"
              :label="item.bankName"
              :value="item.bankCode" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开户银行名称" prop="bankName">
          <el-input readonly="readonly" v-model="editForm.bankName" auto-complete="off" placeholder="请选择" @click.native="getUnioLs()"/>
          <!--<el-select v-model="editForm.interBankNo" placeholder="请选择" filterable style="width:100%">
            <el-option
              v-for="item in unionnos"
              :key="item.bankName"
              :label="item.bankName"
              :value="item.interBankNo"
            >
            </el-option>
          </el-select>-->
        </el-form-item>
        <el-form-item label="账户类型" prop="accType">
          <el-radio-group v-model="editForm.accType">
            <el-radio :label="0" >服务费账户</el-radio>
            <el-radio :label="1" >代付账户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否使用" prop="used">
          <el-radio-group v-model="editForm.used">
            <el-radio :label="0" >否</el-radio>
            <el-radio :label="1" >是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="doCreate">确定</el-button>
        <el-button v-else type="primary" @click="doEdit">确定</el-button>
      </div>
    </el-dialog>

    <!--支行弹窗-->
    <el-dialog :visible.sync="UnionDialogVisible" title="选择开户银行名称" :close-on-click-modal="false">
      <div class="filter-container">
        <el-form :inline="true" status-icon label-position="left" class="demo-form-inline"
                 label-width="100px" style="width: 100%; margin-left:50px;">
          <el-form-item label="开户银行名称" prop="account">
            <el-input auto-complete="off" v-model="editForm1.interBankName" placeholder="开户银行名称" style="width:400px" />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" @click="queryUnio">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="unionnosLoading" :data="unionnos" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <el-table-column align="left" min-width="100" label="开户银行名称" prop="bankName"/>
        <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="choose(scope.row)">选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total1>0" :total="total1" :page.sync="editForm1.pageNum" :limit.sync="editForm1.pageSize"
                  @pagination="getUnionnos"/>
    </el-dialog>

  </div>
</template>

<script>
  import { doGet, doPost } from '@/api'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission'
  import {listHistory} from "../../api/user"; // 权限判断函数

  export default {
    name: 'bank',
      al : '',
      pageNum: '',
      pageSize: '',
  components: { Pagination },
    filters: { },

    data() {
      return {
        list: [],
        banks: [],
        unionnos: [],
        total: 0,
        total1: 0,
        listLoading: true,
        unionnosLoading: true,
        listQuery: {
          // page: 1,
          // limit: 20,
          account: undefined,
          name: undefined,
          bankCode: undefined,
          interBankNo: undefined,
          bankName: undefined,
          accType: undefined,
          used: ''
        },
        textMap: {
          update: '编辑',
          create: '新增银行账户'
        },
        editForm: {
          account: undefined,
          name: undefined,
          bankCode: undefined,
          interBankNo: undefined,
          accType: '',
          used: ''
        },
        editFormRules: {
          account: [{ required: true, message: '账户号不允许为空', trigger: 'blur' }],
          name: [{ required: true, message: '账户名称不允许为空', trigger: 'blur' }],
          bankCode: [{ required: true, message: '银行名称不允许为空', trigger: 'blur' }],
          interBankNo: [{ required: true, message: '开户银行联行号不允许为空', trigger: 'blur' }],
          accType: [{ required: true, message: '账户类型不允许为空', trigger: 'blur' }]
        },
          editForm1: {
            pageNum: 1,
            pageSize: 20,
            bankCode: undefined,
            interBankName: undefined
          },
        editDialogVisible: false,
        dialogStatus: undefined,
        UnionDialogVisible: false
      }

  },
    created() {
      this.getList()
      this.getBanks()
    },

    methods: {
      checkPermission,
      getBanks() {
          doGet('bank.html?op=list', {}).then(response => {
              this.banks = response.rows
          }).catch(() => {
              this.banks = []
          })
      },
      getUnionnos(subankCode, flag) {
          if (typeof(subankCode) == 'string') {
              this.pageNum = 1
              this.pageSize = 20
          } else {
              this.pageNum = subankCode.page
              this.pageSize = subankCode.limit
          }
          this.editForm1 = {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              bankCode: this.al,
              bankName: this.editForm1.interBankName,
              interBankNo: this.editForm.interBankNo,
              flag: flag
          }
          this.UnionDialogVisible = true
          this.unionnosLoading = true
        doGet('unionno.html?op=list', this.editForm1).then(response => {
            this.unionnos = response.rows
            this.total1 = response.data.totalcount
            this.unionnosLoading = false
        }).catch(() => {
            this.unionnos = []
            this.total1 = 0
            this.unionnosLoading = false
        })
      },

      bankNameChange(bankCode) {
        this.al = bankCode.substring(0, 3)
         var flag = '1';
        this.getUnionnos(this.al, flag)
        this.editForm.interBankNo = undefined
      },
      /**获取数据列表 */
      getList() {
        this.listLoading = true
        doGet('account.html?op=list', this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.rows.length
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      queryList() {
        this.listQuery.pageNum = 1
        this.listQuery.pageSize = 20
        this.getList()
      },
      /**新增 */
      handleAdd() {
        this.editForm = {
          account: undefined,
          name: undefined,
          bankCode: undefined,
          interBankNo: this.unionnos.interBankNo,
          accType: 0,
          used: 0
        },
        // this.editDialogVisible = true
        this.dialogStatus = "create"
        this.editDialogVisible = true
        this.$nextTick(() => {
            this.$refs['editForm'].clearValidate()
        })
      },
      /**编辑 */
      handleEdit(row) {
            this.editForm = Object.assign({}, row);
            this.editIndex = this.list.indexOf(row);
            this.dialogStatus = "update"
            this.editDialogVisible = true
            this.$nextTick(() => {
                this.$refs['editForm'].clearValidate()
            })
        },
        /**修改 */
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('account.html?op=chg', this.editForm)
              .then(response => {
                this.editDialogVisible = false
                this.list[this.editIndex] = this.editForm
                this.$notify.success({
                  title: '成功',
                  message: '修改成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      /**添加 */
      doCreate() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('account.html?op=add', this.editForm)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      /**删除 */
      doDelete(row) {
        doGet("account.html?op=del",row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除银行成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.$refs[`popover-` + row.account].doClose()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },
        /**查询银行名称*/
        queryUnio() {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al,'2')
        },
        getUnioLs() {
        if(this.al == null){
            alert('请选择银行名称')
          } else {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '1')
          }
        },
        /** 选择分行*/
        choose(row) {
            this.editForm.interBankNo = row.interBankNo
            this.editForm1.interBankName = row.bankName
            this.editForm.bankName = row.bankName
            this.UnionDialogVisible = false
        },
    }
  }
</script>
